package com.frontop.bigdata.modules.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frontop.bigdata.modules.model.BigDataAccessPageInfo;
import com.frontop.bigdata.modules.model.BigDataProject;
import com.frontop.bigdata.modules.service.BigDataAccessPageInfoService;
import com.frontop.bigdata.modules.service.BigDataAccessPageService;
import com.frontop.bigdata.modules.service.BigDataOverviewService;
import com.frontop.bigdata.modules.service.BigDataProjectService;
import com.frontop.bigdata.modules.vo.BigDataProjectVO;
import com.frontop.common.domain.UserJurisdiction;
import com.frontop.common.util.JwtUtil;
import com.frontop.common.util.ResultJsonUtil;
import com.frontop.dubbo.api.auth.Oauth2Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 大数据统计--站点列表 前端控制器
 * </p>
 *
 * @author YangJia
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/big-data-project")
public class BigDataProjectController {

    @Autowired
    BigDataProjectService bigDataProjectService;

    @Autowired
    BigDataOverviewService bigDataOverviewService;
    @Autowired
    BigDataAccessPageInfoService bigDataAccessPageInfoService;
    @Autowired
    BigDataAccessPageService bigDataAccessPageService;

    @Reference
    private Oauth2Api oauth2Api;
    @ApiOperation(value = "网点列表")
    @PostMapping("/siteList")
    public ResultJsonUtil<Object> siteList(@RequestBody(required = false) BigDataProjectVO vo,
                                                   Authentication authentication,
                                                   HttpServletRequest request) {
        Page<BigDataProject> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        QueryWrapper<BigDataProject> queryWrapper = new QueryWrapper<>();

        if(StringUtils.isNotEmpty(vo.getProjectName())){
            queryWrapper.like("project_name",vo.getProjectName());
        }
        if(vo.getOrganizationId() != null){
            queryWrapper.eq("organization_id",vo.getOrganizationId());
        }else {
            queryWrapper.isNull("organization_id");
        }
        queryWrapper.orderByDesc("create_time");
        page = bigDataProjectService.page(page,queryWrapper);
        return new ResultJsonUtil().success(page);
    }


    @ApiOperation(value = "网页列表中左侧的网点列表API")
    @GetMapping("/siteListMuen")
    public ResultJsonUtil<Object> siteListMuen(Authentication authentication,
                                           HttpServletRequest request) {
        QueryWrapper<BigDataProject> queryWrapper = new QueryWrapper<>();
        //获取当前登录用户基本权限信息
        UserJurisdiction  userJurisdiction = JwtUtil.getUserJurisdiction(authentication);
        queryWrapper.in("organization_id",userJurisdiction.getLowerOrganizationIds());

        return new ResultJsonUtil().success(bigDataProjectService.list(queryWrapper));
    }

    @ApiOperation(value = "拉取新网点")
    @GetMapping("/pullProjectList")
    @PreAuthorize("hasAuthority('bigdata:site:pullProjectList')")
    public ResultJsonUtil<Object> pullProjectList(Authentication authentication,
                                                           HttpServletRequest request) {
        bigDataProjectService.pullProjectList();
        return new ResultJsonUtil().success("成功");
    }

    @ApiOperation(value = "修改网点信息")
    @PostMapping("/updateSite")
    @PreAuthorize("hasAuthority('bigdata:site:update')")
    public ResultJsonUtil<Object> updateSite(@RequestBody(required = false) BigDataProject bigDataProject,
                                           Authentication authentication,
                                           HttpServletRequest request) {
        if(bigDataProject.getOrganizationId().equals(0)){
            bigDataProject.setOrganizationId(null);
        }
        if(bigDataProject.getOrganizationId() != null){
            QueryWrapper<BigDataProject> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("organization_id",bigDataProject.getOrganizationId());
            BigDataProject dataProject = bigDataProjectService.getOne(queryWrapper);
            if(dataProject != null && !dataProject.getSiteId().equals(bigDataProject.getSiteId())){
                return new ResultJsonUtil().customFail("该企业已经存在网点");
            }
        }

        if(bigDataProject != null){
            BigDataProject addData = new BigDataProject();
            addData.setSiteId(bigDataProject.getSiteId());
            addData.setProjectName(bigDataProject.getProjectName());
            addData.setPersonPhone(bigDataProject.getPersonPhone());
            addData.setPersonCharge(bigDataProject.getPersonCharge());
            addData.setOrganizationId(bigDataProject.getOrganizationId());
            bigDataProjectService.updateById(addData);
        }

        return new ResultJsonUtil().success("修改成功");
    }


    @ApiOperation(value = "删除网点")
    @GetMapping("/deleteSite/{siteId}")
    @PreAuthorize("hasAuthority('bigdata:site:delete')")
    public ResultJsonUtil<Object> updateSite(@PathVariable("siteId") Long siteId,
                                             Authentication authentication,
                                             HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> pageMap = new HashMap<>();
        map.put("site_id",siteId);
        //查询页数据
        List<BigDataAccessPageInfo> bigDataAccessPageInfos =  bigDataAccessPageInfoService.listByMap(map);
        //循环页信息并删除统计数据
        bigDataAccessPageInfos.forEach(pageInfo->{
            pageMap.put("page_id",pageInfo.getPageId());
            //删除页统计数据
            bigDataAccessPageService.removeByMap(pageMap);
        });
        //删除页信息数据
        bigDataAccessPageInfoService.removeByMap(map);
        //删除总览数据
        bigDataOverviewService.removeByMap(map);
        //删除站点
        bigDataProjectService.removeById(siteId);
        return new ResultJsonUtil().success("修改成功");
    }



}
